package com.aboat.licenseVerify;

import com.aboat.view.WindowFrame;
import de.schlichtherle.license.*;

import java.io.File;
import java.util.Date;
import java.util.prefs.Preferences;

/**
 * 许可认证
 *
 * @author aboat
 * @email clear_windy@yeah.net
 * @create 2017-04-12 19:51
 */
public class Permitted {
    public final static String PUBLICALIAS = "publiccert";//公有证书别名
    public final static String PUBLIC_STORE = "/publicCerts.store";//公用仓库地址
    public final static String STOREPWD = "xyj802819";//证书仓库秘钥
    public final static String KEY = "xyj802819";//证书秘钥
    public final static String SUBJECT = "程序注册";//主题
    public static String STOREPATH = System.getProperty("user.home")+"/spider.lic";//证书地址

    public static PermittedMessage verifyLicense(String storePath){
        if(storePath != null){
            STOREPATH = storePath;
        }

        PermittedMessage pm = new PermittedMessage();
        LicenseManager licenseManager = LicenseManagerHolder.getLicenseManager(initLicenseParams());
        try {
            LicenseContent content = licenseManager.install(new File(STOREPATH));
            String info = content.getInfo();
            Date notBefore = content.getNotBefore();
            Date notAfter =content.getNotAfter();
            String md5Info = (String)content.getExtra();
            licenseManager.verify();

            if(cpInfo.verifyCpInfo(md5Info)){
                pm.setPermitted(true);
                pm.getMessage().add(info);
            }else{
                pm.setPermitted(false);
                pm.getMessage().add("该电脑未授权，一份许可只允许一台电脑使用!");
            }

            Date networkTime = DateTool.getNetworkTime();
            if(networkTime == null) {
                pm.setPermitted(false);
                pm.getMessage().add("该电脑未连接到网络，请连接网络后重启本软件!");
            }else {
                if (networkTime.compareTo(notBefore) > -1 && networkTime.compareTo(notAfter) < 0) {
                    pm.setPermitted(true);
                    pm.getMessage().add("授权有效期："+DateTool.dateToString(notBefore,"yyyy-MM-dd HH:mm")+"至"+DateTool.dateToString(notAfter,"yyyy-MM-dd HH:mm"));
                }
            }
        } catch (Exception e){
            pm.setPermitted(false);
            pm.getMessage().add("许可无效！请联系卖家获取有效许可文件进行注册"+"\r\n");
            pm.getMessage().add("未注册用户只可爬取"+WindowFrame.maxNum+"张图片！"+"\r\n");
        }

        /*控制台展示许可信息*/
        if(pm.getPermitted()){
            WindowFrame.maxNum = null;
        }
        WindowFrame.textContent.setText("");
        for(String message : pm.getMessage()) {
            WindowFrame.textContent.append(message+"\r\n");
        }
        return pm;
    }

    private static LicenseParam initLicenseParams() {
        Preferences preference = Preferences.userNodeForPackage(Permitted.class);
        CipherParam cipherParam = new DefaultCipherParam(KEY);
        KeyStoreParam privateStoreParam  = new DefaultKeyStoreParam(Permitted.class, PUBLIC_STORE, PUBLICALIAS, STOREPWD, null);
        LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT, preference, privateStoreParam, cipherParam);
        return licenseParams;
    }
}
